
#include "Sync.h"


/*------------------------------------------------------------------------------------------------------------------------------------------
--		SOURCE FILE:	Sync.cpp	-		Represents the "Network Layer of the OSI Model.  Handles Syncronization of the line. 
--
--		PROGRAM:		Wireless Modem Protocol Driver
--
--		FUNCTIONS:		bool randomWaitTimer(PTTYINFO pinfo)
--						bool ackTimer(PTTYINFO pinfo)
--						void delay(PTTYINFO pinfo)
--						bool waitFrame(PTTYINFO pinfo)
--						bool waitENQ(PTTYINFO pinfo)
--						
--						
--		DATE:			November 25th, 2011
--
--		REVISIONS:		
--
--		DESIGNER:		Mike Zobac
--
--		PROGRAMMER		Chris Sim
--
--		NOTES:			
--------------------------------------------------------------------------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION: Random Wait Timer		
--
--		DATE: November 28, 2011			
--
--		DESIGNER: 	Mike Zobac	
--
--		PROGRAMMER:	Mike Zobac	
--
--		INTERFACE: bool randomWaitTimer(PTTYINFO pinfo)		
--
--		RETURNS:   returns true if an ENQ is received during the timer, and false if it doesn't.
--
--		NOTES:	    Sets the receiving variable in the TTYINFO global data store to true if an ENQ is received.	
--------------------------------------------------------------------------------------------------------------------------------------------*/

bool randomWaitTimer(PTTYINFO pinfo)
{
    return (WaitForSingleObject(pinfo->wait, RANDOMTIME) == WAIT_OBJECT_0);
}

/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION: ACK Timer		
--
--		DATE: November 28, 2011			
--
--		DESIGNER: Mike Zobac	
--
--		PROGRAMMER:	Mike Zobac	
--
--		INTERFACE: bool ackTimer(PTTYINFO pinfo)		
--
--		RETURNS:   true if an ACK is received before the time runs out.
--
--		NOTES:	   	
--------------------------------------------------------------------------------------------------------------------------------------------*/

bool ackTimer(PTTYINFO pinfo)
{
	return (WaitForSingleObject(pinfo->ACK, ACKTIME) == WAIT_OBJECT_0);
}

/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION: delay 		
--
--		DATE: November 23, 2011			
--
--		DESIGNER: 		
--
--		PROGRAMMER:		
--
--		INTERFACE: void delay()		
--
--		RETURNS:   After waiting for 150ms	
--
--		NOTES:			
--------------------------------------------------------------------------------------------------------------------------------------------*/

void delay(PTTYINFO pinfo)
{
   (WaitForSingleObject(pinfo->delay, DELAY) == WAIT_OBJECT_0);
}

/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION: Wait Frame 		
--
--		DATE: November 23, 2011			
--
--		DESIGNER: 		
--
--		PROGRAMMER:		
--
--		INTERFACE: bool waitFrame()	
--
--		RETURNS:   true if and ACK is received before the timer expires, and false if the timer expires	
--
--		NOTES:			
--------------------------------------------------------------------------------------------------------------------------------------------*/

bool waitFrame(PTTYINFO pinfo) {
	return (WaitForSingleObject(pinfo->frameBuilt, FRAMETIME) == WAIT_OBJECT_0);
}

/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION: Wait Frame 		
--
--		DATE: November 23, 2011			
--
--		DESIGNER: 		
--
--		PROGRAMMER:		
--
--		INTERFACE: bool waitFrame()	
--
--		RETURNS:   true if and ACK is received before the timer expires, and false if the timer expires	
--
--		NOTES:			
--------------------------------------------------------------------------------------------------------------------------------------------*/

bool waitENQ(PTTYINFO pinfo) {
	return (WaitForSingleObject(pinfo->ENQ, RANDOMTIME) == WAIT_OBJECT_0);
}